package com.tedu.ivos.vehicle.service.impl;

import com.tedu.ivos.vehicle.mapper.VehicleMapper;
import com.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import com.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import com.tedu.ivos.vehicle.pojo.entity.Vehicle;
import com.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import com.tedu.ivos.vehicle.service.IVehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements IVehicleService {

    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("车辆查询业务，参数：{}",vehicleQuery);
        return vehicleMapper.selectVehicle(vehicleQuery);
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("新增车辆业务，参数：{}",vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if(vehicle.getId()==null){
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            vehicle.setCreateTime(new Date());
            vehicleMapper.insertVehicle(vehicle);
        }else {
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("删除车辆业务，参数：{}",id);
        vehicleMapper.delete(id);
    }

    @Override
    public void unbindVehicle(Long id) {
        log.debug("解绑车辆业务，参数：{}",id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.unbindVehicle(vehicle);
    }

    @Override
    public void bindVehicle(Long geofenceId, Long id) {
        log.debug("绑定车辆业务，参数：{},{}",geofenceId,id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geofenceId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }
}
